package com.client;

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.codehaus.jettison.json.JSONException;

import com.alibaba.fastjson.JSONObject;
import com.entity.MyClass;
import com.entity.Student;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;

public class MyClient {
	 public static void main(String[] argc) throws JSONException {
//		    output3();
//		 xmlToObj(output3());
		 josnToObj(output2());
	 }
	 //输出普通字符串
	 public static void output1(){
		    Client c = Client.create();  
	        WebResource r=c.resource("http://localhost:8080/Spring-Jersey/ws/jersey/9999");  
	        MultivaluedMapImpl params = new MultivaluedMapImpl();  
	        params.add("name", "east");  
	        System.out.println(r.queryParams(params).get(String.class));
	 }
	 //输出json
	 public static String output2(){
		    Client c = Client.create();  
	        WebResource r=c.resource("http://localhost:8080/Spring-Jersey/ws/jersey/student");  
	        MultivaluedMapImpl params = new MultivaluedMapImpl();  
	        params.add("name", "east");  
	        return r.queryParams(params).get(String.class);
	 }
	 
	//输出xml
		 public static String output3(){
			    Client c = Client.create();  
		        WebResource r=c.resource("http://localhost:8080/Spring-Jersey/ws/jersey/xml");  
		        MultivaluedMapImpl params = new MultivaluedMapImpl();  
		        params.add("name", "east");  
		        return r.queryParams(params).get(String.class);
		 }
	//xml转成对象
		 public static void xmlToObj(String xml){
			 MyClass myClass = null;  
		        try {  
		            JAXBContext context = JAXBContext.newInstance(MyClass.class);  
		            // 进行将Xml转成对象的核心接口  
		            Unmarshaller unmarshaller = context.createUnmarshaller();  
		            StringReader sr = new StringReader(xml);  
		            myClass= (MyClass)unmarshaller.unmarshal(sr);  
		        } catch (JAXBException e) {  
		            e.printStackTrace();  
		        }  
		        System.out.println(myClass.getClassName());
		 }
		 
		//json字符串转成java对象
		 public static void josnToObj(String json){
			 Student student=null;
			 student=(Student)JSONObject.parseObject(json, Student.class);
			 System.out.println(student.getName());
		 }
}
